import { Component, OnInit } from '@angular/core';
import { VolumeService } from 'src/app/service/volume.service';

@Component({
  selector: 'app-statist-report-town',
  templateUrl: './statist-report-town.component.html',
  styleUrls: ['./statist-report-town.component.scss']
})
export class StatistReportTownComponent implements OnInit {
  waterSum: any = 0;
  data: any[];
  searchTimeType = '日';
  searchTime = new Date();
  genTwoLengthNumberString = (n) => (n >= 10 ? n : '0' + n);
  dateTimeTrans(d) {
      if (!d) {
          return '';
      }
      let yy = d.getFullYear();
      let MM = this.genTwoLengthNumberString(d.getMonth() + 1);
      let dd = this.genTwoLengthNumberString(d.getDate());
      if (this.searchTimeType === '日') {
          return yy + '-' + MM + '-' + dd;
      }
      if (this.searchTimeType === '月') {
          return yy + '-' + MM;
      }
      if (this.searchTimeType === '年') {
          return yy;
      }
  }
  timeTypeTrans(str) {
      if (str === '日') {
          return 'day';
      } else if (str === '月') {
          return 'month';
      } else if (str === '年') {
          return 'year';
      }
      return 'unrecgonized';
  }
  async search() {
      // const res = await fetchStatisticsList(searchTimeType.value, dateTimeTrans(searchTime.value), searchZone.value)
      const res = await this.volumeService.fetchTownVolumeByTime({
          type: this.timeTypeTrans(this.searchTimeType),
          currentTime: this.dateTimeTrans(this.searchTime),
      });
      console.log(res);
      this.waterSum = (res.data || []).reduce((sum, item) => {
          return sum + item.volume;
      }, 0);
      this.data = res.data;
  }
  constructor(private volumeService: VolumeService) {}
  timeTypes = [
      {
          value: '日',
          lable: '日报',
      },
      {
          value: '月',
          lable: '月报',
      },
      {
          value: '年',
          lable: '年报',
      },
  ];
  handleReset = () => {
      this.searchTimeType = '日';
      this.searchTime = new Date();
      this.search();
  };

  ngOnInit(): void {
      this.search();
  }
}
